/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.view.reserveView;
/**
 * 说明：
 * 出入库单查询
 * @Author：
 * @Date：2012-03-21
 */
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;

import com.ruipengkj.ApplicationContext;
import com.ruipengkj.bean.TOutBill;
import com.ruipengkj.bean.TStockInOut;
import com.ruipengkj.bean.TStore;
import com.ruipengkj.bean.TStoreTransfer;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.base.query.PageIndex;
import com.ruipengkj.commons.base.query.PageModel;
import com.ruipengkj.commons.base.query.PageView;
import com.ruipengkj.commons.util.TabelUtil;
import com.ruipengkj.component.DataPackageComponent;
import com.ruipengkj.component.JComboBoxPOJO;
import com.ruipengkj.component.JLabelJComboBoxComponent;
import com.ruipengkj.component.RPJButtonBlue;
import com.ruipengkj.component.RPJButtonGray;
import com.ruipengkj.component.jpanel.QueryPanel;
import com.ruipengkj.component.page.PagingComponent;
import com.ruipengkj.factory.ImageFactory;
import com.ruipengkj.model.TStockInOutModel;
import com.ruipengkj.service.TOutBillService;
import com.ruipengkj.service.TStockInOutService;
import com.ruipengkj.service.TStoreService;
import com.ruipengkj.service.TStoreTransferService;
import com.ruipengkj.view.base.BasePagePanel;
import com.ruipengkj.view.orderView.MarketReturnedAddOrModifyDialog;
import com.ruipengkj.view.orderView.OrderDetailDialog;
import com.ruipengkj.view.purchaseView.PurchaseEnterAddOrModifyPanel;
import com.ruipengkj.view.purchaseView.PurchaseRetreatAddPanel;

public class StockInOutQuery extends BasePagePanel<TStockInOut> implements Runnable{

	private static final long serialVersionUID = 1L;
	
	private JLabelJComboBoxComponent reserveID;//仓库编号下拉框
	
	private JLabelJComboBoxComponent filte;//条件过滤所有、出库、入库
	private JLabelJComboBoxComponent sourceType;//原因类型
	
	private DataPackageComponent startTime; //查询   开始时间段
	private DataPackageComponent endTime; //结束时间段
	
	private JButton queryButton;//查询
	private JButton clearButton;//清除按钮
	
	private JTable table;
	private JScrollPane tableJsp;
	private TStockInOutModel tStockInOutModel;
	private TStoreService tStoreService;
	
	private PagingComponent pagingComponent;//翻页组件
	private PageView<TStockInOut> pageView;//翻页主键
	private TStockInOutService tStockInOutService;
	
	private TOutBillService tOutBillService;//物品领用
	private TStoreTransferService tStoreTransferService;//商品调拨
	public StockInOutQuery() {
		this.setLayout(new BorderLayout());
		this.init();
		this.initListener();
	}
	private void init() {
		tStoreTransferService = BuildSpringApplication.getInstance().getTStoreTransferServiceImpl();
		tOutBillService = BuildSpringApplication.getInstance().getTOutBillServiceImpl();
		tStoreService = BuildSpringApplication.getInstance().getTStoreServiceImpl();
		tStockInOutService = BuildSpringApplication.getInstance().getTStockInOutServiceImpl();
		tStockInOutModel = new TStockInOutModel();
		table=uiFactory.createRPJTable(tStockInOutModel, null, this, 30, JLabel.CENTER);
		tableJsp=uiFactory.createJScrollPane(table);
		
		TabelUtil.hideTableColumn(5, table);//隐藏原因单据编号
		
		pageView = new PageView<TStockInOut>();
		pagingComponent=new PagingComponent();
		pagingComponent.setPageDelegale(this);
		add(initTop(), BorderLayout.NORTH);
		add(tableJsp, BorderLayout.CENTER);
		add(pagingComponent, BorderLayout.SOUTH);
		
		SwingUtilities.invokeLater(queryStore);
	}
	private JPanel initTop() {
		QueryPanel queryPanel=new QueryPanel("出入库单查询",ImageFactory.loadImage(ImageFactory.FUN_5_ICON_6));
		
		JPanel top_panel = uiFactory.createGridJPanel(2, 1);
		
		queryButton = new RPJButtonBlue("查询");
		clearButton=new RPJButtonGray("清空");
		reserveID = new JLabelJComboBoxComponent("仓库编号：", new String[]{}, 188);
		//1为采购所引起的入库，原因单为采购单编号 2为采购退货所引起的出库，原因单为退货单
		//3为销售定单所引起的出库，原因单为销售定单 4为销售退货所引起的入库，原因单为销售退货单
		//5为员工领用所引起的出库，原因单为领用单 6为报损所引起的出库，原因单为报损单 
		//7为仓库调度所引起的出库，原因单为仓库调拨单 8为仓库调度所引起的入库，原因单为仓库调拨单 
		//9为生产材料领用出库 10为生产成品入库 100为未知原因的入库 101为未知原因的出库
		filte = new JLabelJComboBoxComponent("过滤条件：", new String[]{"所有", "1、入库", "2、出库"}, 188);
		sourceType = new JLabelJComboBoxComponent("", new String[]{
				"所有", "1、采购入库", "2、采购退货出库", "3、销售出库", "4、销售退货入库", "5、员工领用出库"
				, "6、报损出库", "7、仓库调拨出库", "8、仓库调拨入库", "9、生成材料领用出库", "10、生产成品入库"
				, "100、未知原因入库", "101、未知原因出库"
		}, 188);
		JPanel jp1 = uiFactory.createFlowJPanel(FlowLayout.LEFT);
		jp1.add(reserveID);
		jp1.add(filte);
		jp1.add(sourceType);
		jp1.add(queryButton);
		jp1.add(clearButton);
		
		startTime = new DataPackageComponent("开始时间：", 15);
		endTime = new DataPackageComponent("结束时间：", 15);
		JPanel jp4 = uiFactory.createFlowJPanel(FlowLayout.LEFT);
		jp4.add(startTime);
		jp4.add(endTime);
		
		top_panel.add(jp1);
		//top_panel.add(jp3);
		top_panel.add(jp4);
		
		queryPanel.addPanel(top_panel);
//		queryPanel.addBorderPanel(top_panel);
		return queryPanel;
	}
	
	
	private void initListener() {
		queryButton.addActionListener(this);
		clearButton.addActionListener(this);
	}
	
	
	
	//查询仓库
	private Runnable queryStore=new Runnable() {
		@Override
		public void run() {
			reserveID.addElement(new JComboBoxPOJO("请选择仓库"));
			try {
				List<TStore> list=tStoreService.findService(null);
				if(list!=null&&list.size()!=0){
					for(int i=0;i<list.size();i++){
						TStore store=list.get(i);
						reserveID.addElement(new JComboBoxPOJO(store.getStoreID(),store.getName(),store));
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	};
	
	@Override
	public void run() {
		this.termQuery(1);
	}
	
	//条件查询
	@Override
	protected void termQuery(final int currentPage){
		final TStockInOut tStockInOut=new TStockInOut();
		//添加条件：仓库
		JComboBoxPOJO storePo=(JComboBoxPOJO)reserveID.getBox().getSelectedItem();//仓库
//		System.out.print(((TStore)storePo.getObject()).getName());
		tStockInOut.setTStore((TStore)storePo.getObject());
		//添加条件：出入/入库
		if (filte.getBox().getSelectedItem().toString().equals("2、出库")) {
			tStockInOut.setType((byte)2);
		} else if (filte.getBox().getSelectedItem().toString().equals("1、入库")) {
			tStockInOut.setType((byte)1);
		} else {
			tStockInOut.setType((byte)0);
		}
		//添加时间条件
		tStockInOut.setHappenTime(startTime.getDate());
		tStockInOut.setEndTime(endTime.getDate());
		//添加条件：出入原因类型
		if (sourceType.getBox().getSelectedItem().toString().equals("1、采购入库")) {
			tStockInOut.setSourceType((byte)1);
		} else if (sourceType.getBox().getSelectedItem().toString().equals("2、采购退货出库")) {
			tStockInOut.setSourceType((byte)2);
		} else if (sourceType.getBox().getSelectedItem().toString().equals("3、销售出库")) {
			tStockInOut.setSourceType((byte)3);
		} else if (sourceType.getBox().getSelectedItem().toString().equals("4、销售退货入库")) {
			tStockInOut.setSourceType((byte)4);
		} else if (sourceType.getBox().getSelectedItem().toString().equals("5、员工领用出库")) {
			tStockInOut.setSourceType((byte)5);
		} else if (sourceType.getBox().getSelectedItem().toString().equals("6、报损出库")) {
			tStockInOut.setSourceType((byte)6);
		} else if (sourceType.getBox().getSelectedItem().toString().equals("7、仓库调拨出库")) {
			tStockInOut.setSourceType((byte)7);
		} else if (sourceType.getBox().getSelectedItem().toString().equals("8、仓库调拨入库")) {
			tStockInOut.setSourceType((byte)8);
		} else if (sourceType.getBox().getSelectedItem().toString().equals("9、生成材料领用出库")) {
			tStockInOut.setSourceType((byte)9);
		} else if (sourceType.getBox().getSelectedItem().toString().equals("10、生产成品入库")) {
			tStockInOut.setSourceType((byte)10);
		} else if (sourceType.getBox().getSelectedItem().toString().equals("100、未知原因入库")) {
			tStockInOut.setSourceType((byte)100);
		} else if (sourceType.getBox().getSelectedItem().toString().equals("101、未知原因出库")) {
			tStockInOut.setSourceType((byte)101);
		} else {
			tStockInOut.setSourceType((byte)0);
		}
		new Thread(){
			public void run(){
				tableJsp.getViewport().remove(table);
				tableJsp.getViewport().add(waitComponent);
				try {
					pageView.setCurrentPage(currentPage);
					PageModel<TStockInOut> pageModel = tStockInOutService.getScrollService(pageView.getFirstResult(), pageView.getMaxResult(),tStockInOut);
					pageView.setPageModel(pageModel);
					pageView.setPageIndex(PageIndex.getPageIndex(pageView.getPageCode(), pageView.getCurrentPage(), pageView.getTotalPage()));
					if(pageModel!=null){
						tStockInOutModel.setRowData(pageModel.getResultList());
						SwingUtilities.invokeLater(new Runnable() {
							@Override
							public void run() {
								tableJsp.getViewport().remove(waitComponent);
								tableJsp.getViewport().add(table);
								TabelUtil.setJTabelAutoClumnWidth(table);
								pagingComponent.show();
							}
						});
					}else{
						//没有订单数据
						tableJsp.getViewport().remove(waitComponent);
						tableJsp.getViewport().add(table);
						tStockInOutModel.clear();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}.start();
	}
	@Override
	protected PageView<TStockInOut> getPageView() {
		return pageView;
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == queryButton) {
			termQuery(1);
		} else if(e.getSource() == clearButton) {
			this.clear();
		}
	}
//双击记录时弹出详细数据
	public void mouseClicked(MouseEvent e) {
		if (e.getSource() == table && e.getClickCount() == 2) {
			String source = (String)table.getValueAt(table.getSelectedRow(), 5);
			int SourceType = ApplicationContext.STOCK_IN_OUT_SOURCE_TYPE_MAP(table.getValueAt(table.getSelectedRow(), 3).toString());
			try {
				switch(SourceType) {
					case 1:
						PurchaseEnterAddOrModifyPanel purchaseEnterAddPanel = new PurchaseEnterAddOrModifyPanel(null, "采购单详细", true);
						purchaseEnterAddPanel.showDialog(source);
						return;
					case 2:
						PurchaseRetreatAddPanel purchaseRetreatAddPanel = new PurchaseRetreatAddPanel(null, "采购退货单详细", true);
						purchaseRetreatAddPanel.showDialog(source);
						return;
					case 3:
						OrderDetailDialog orderDetailDialog = new OrderDetailDialog(null, "销售详细", true);
						orderDetailDialog.showDialog(source);
						return;
					case 4:
						MarketReturnedAddOrModifyDialog marketReturnedAddOrModifyDialog = new MarketReturnedAddOrModifyDialog(null,"销售退货详情",true);
						marketReturnedAddOrModifyDialog.showDialog(source);
						return;
					case 5:
						TOutBill tOutBill = tOutBillService.getByIdService(source);
						ArticlesUsingAddDialog articlesUsingAddDialog = new ArticlesUsingAddDialog(null, "领用单详情", true);
						articlesUsingAddDialog.showDialog(tOutBill);
						return;
					case 6:
						TOutBill damage = tOutBillService.getByIdService(source);
						CommodityDamageAddDialog commodityDamageAddDialog = new CommodityDamageAddDialog(null, "报损单详情", true);
						commodityDamageAddDialog.showDialog(damage);
						return;
					case 7:
					case 8:
						TStoreTransfer tStoreTransfer = tStoreTransferService.getByIdService(source);
						StoreTransferPanelAddDialog StoreTransferPanelAddDialog = new StoreTransferPanelAddDialog(null, "仓库调度所引起的入库", true);
						StoreTransferPanelAddDialog.showDialog(tStoreTransfer);
						return;
					case 101:
						OtherReserveAddDialog OtherReserveAddDialog = new OtherReserveAddDialog(null, "其他方式出库单详情", true);
						OtherReserveAddDialog.showDialog(source);
						return;
				}
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}
	}
	@Override
	public void clear() {
		tStockInOutModel.clear();
		reserveID.getBox().setSelectedIndex(0);//仓库编号下拉框
		filte.getBox().setSelectedIndex(0);//条件过滤所有、出库、入库
		sourceType.getBox().setSelectedIndex(0);//原因类型
		startTime.setDate(null); //查询   开始时间段
		endTime.setDate(null); //结束时间段
		
	}
}
